package com.fgw.service.impl;

import com.fgw.common.ServerResponse;
import com.fgw.dao.CommunicationMapper;
import com.fgw.pojo.Communication;
import com.fgw.service.ICommunicationService;
import com.fgw.util.DateTimeUtil;
import com.fgw.vo.CommunicatonListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lyj on 2017/9/13.
 */
@Service("iCommunicationService")
public class CommunicationServiceImpl implements ICommunicationService{

   @Autowired
   private CommunicationMapper communicationMapper;

    /**
     * 前台互动交流列表
     * @return
     */
    @Override
    public ServerResponse<CommunicatonListVo> getALListCommunication() {
        List<Communication> communicationList =communicationMapper.getALLCommunicationList();

        List<Communication> list =new ArrayList<>();
        for (Communication temp:communicationList){
            Communication communication=new Communication();
            communication.setcId(temp.getcId());
            communication.setcFbAuthor(temp.getcFbAuthor());
            communication.setcTitle(temp.getcTitle());
            communication.setcEntime_v(DateTimeUtil.dateToStr(temp.getcEntime(),"yyyy-MM-dd"));
            communication.setcFbTime_v(DateTimeUtil.dateToStr(temp.getcFbTime(),"yyyy-MM-dd"));
            communication.setcStatus(temp.getcStatus());
            list.add(communication);
        }

        CommunicatonListVo communicatonListVo=new CommunicatonListVo();
        communicatonListVo.setCommunicationList(list);

        return  ServerResponse.createBySuccess(communicatonListVo);
    }

    /**
     * 后台分页获取互动交流列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ServerResponse<PageInfo> getNeedCommunicationList(Integer pageNum, Integer pageSize) {

        //开始分页
        PageHelper.startPage(pageNum,pageSize);
        List<Communication> communicationList=communicationMapper.getNeedCommunicationList();

        //封装所需的参数
        List<Communication> list =new ArrayList<>();
        for (Communication temp:communicationList){
            Communication communication=new Communication();
            communication.setcId(temp.getcId());
            communication.setcFbAuthor(temp.getcFbAuthor());
            communication.setcTitle(temp.getcTitle());
            communication.setcEntime_v(DateTimeUtil.dateToStr(temp.getcEntime(),"yyyy-MM-dd"));
            communication.setcFbTime_v(DateTimeUtil.dateToStr(temp.getcFbTime(),"yyyy-MM-dd"));
            communication.setcStatus(temp.getcStatus());
            communication.setPersonjoin(temp.getPersonjoin());
            //communication.setTeachers(temp.getTeachers());
            list.add(communication);
        }

        PageInfo pageReslut=new PageInfo(communicationList);
        pageReslut.setList(list);

        return ServerResponse.createBySuccess(pageReslut);
    }

}
